﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using LantinxLBS.Common;
using System.Diagnostics;
using LantinxShare.Common;
using LantinxShare.LBS;

namespace LantinxLBS.Views
{
    public partial class FrmRole2 : ChildWindow
    {
        public FrmRole2()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(FrmRole2_Loaded);
        }

        void FrmRole2_Loaded(object sender, RoutedEventArgs e)
        {
            LoadRole();
            LoadUsers();
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            AddRole();
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            UpdateRole();
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            DeleteRole();
        }

        public void CloseInvoke(Action action)
        {
            action();
        }

        private void DeleteRole()
        {
            if (this.cmbList.SelectedIndex < 0)
            {
                MsgHelper.Show("请选择一个角色");
                return;
            }
            GRole role = this.cmbList.SelectedItem as GRole;
            PathHelper.GetData("AddressPage.aspx?option=" + GConstant.OP_DEL_ROLE,
                new Dictionary<string, string>(){
                {"roleId",role.RoleId.ToString()}
                }, (e) =>
            {
                this.Dispatcher.BeginInvoke(() =>
                {
                    LoadRole();
                });
            });

        }

        private void UpdateRole()
        {
            if (string.IsNullOrEmpty(this.txtRoleName.Text.Trim()))
            {
                MsgHelper.Show("角色名不能为空");
                return;
            }
            if (this.cmbList.SelectedIndex < 0)
            {
                MsgHelper.Show("请选择一个角色");
                return;
            }
            GRole role = this.cmbList.SelectedItem as GRole;
            PathHelper.GetData("AddressPage.aspx?option=" + GConstant.OP_EDIT_ROLE,
                new Dictionary<string, string>(){
                {"roleName",this.txtRoleName.Text.Trim()},
                {"roleId",role.RoleId.ToString()}
                }, (e) =>
            {
                this.Dispatcher.BeginInvoke(() =>
                {
                    LoadRole();
                });
            });

        }

        private void AddRole()
        {
            if (string.IsNullOrEmpty(this.txtRoleName.Text.Trim()))
            {
                MsgHelper.Show("角色名不能为空");
                return;
            }

            PathHelper.GetData("AddressPage.aspx?option=" + GConstant.OP_SET_ROLE, "roleName", this.txtRoleName.Text.Trim(), (e) =>
            {
                this.Dispatcher.BeginInvoke(() =>
                {
                    LoadRole();
                });
            });
        }

        private void LoadRole()
        {

            PathHelper.GetData("AddressPage.aspx?option=" + GConstant.OP_GET_ROLE, (e) =>
            {
                List<GRole> roleList = new List<GRole>();
                List<string[]> list = G.GetSplit(e.Result);

                foreach (var item in list)
                {
                    roleList.Add(new GRole()
                    {
                        Id = int.Parse(item[0]),
                        RoleId = int.Parse(item[1]),
                        RoleName = item[2]
                    });
                }

                this.Dispatcher.BeginInvoke(() =>
                {
                    this.cmbList.ItemsSource = roleList;
                    this.cmbChooseRole.ItemsSource = roleList;
                });
            });
        }

        private void btnAddUser_Click(object sender, RoutedEventArgs e)
        {
            AddUser();
        }

        private void btnEditUser_Click(object sender, RoutedEventArgs e)
        {
            EditUser();
        }

        private void LoadUsers()
        {
            DataHelper.LoadUser((result) =>
            {
                this.Dispatcher.BeginInvoke(() =>
                {
                    this.lstUsers.ItemsSource = result;
                });

            });
        }



        private void AddUser()
        {
            GRole role = this.cmbChooseRole.SelectedItem as GRole;
            //PathHelper.GetData("AddressPage.aspx?option=" + GConstant.OP_SET_USER,
            //    new Dictionary<string, string>(){
            //    {"UserName",this.txtUserName.Text},
            //    {"UserAlias",this.txtUserAlias.Text},
            //    {"Password",this.txtPassword.Text},
            //    {"Phone",this.txtPhone.Text},
            //    {"RoleId",role.RoleId.ToString()}
            //    }, (e) =>
            //        {
            //            List<GUser> userList = ConvertToUser(e);
            //            this.Dispatcher.BeginInvoke(() =>
            //            {
            //                LoadUsers();
            //                this.lstUsers.SelectedIndex = -1;
            //                this.cmbChooseRole.SelectedIndex = -1;
            //                this.txtUserName.Text = string.Empty;
            //                this.txtUserAlias.Text = string.Empty;
            //                this.txtPassword.Text = string.Empty;
            //                this.txtPassword2.Text = string.Empty;
            //                this.txtPhone.Text = string.Empty;

            //            });
            //        });

            string userName = this.txtUserName.Text.Trim();
            string userAlias = this.txtUserAlias.Text.Trim();
            string password = this.txtPassword.Password.Trim();
            string password2 = this.txtPassword2.Password.Trim();
            string phone = this.txtPhone.Text.Trim();

            if (string.IsNullOrEmpty(userName))
            {
                MsgHelper.Show("用户名不能为空");
                return;
            }

            if (string.IsNullOrEmpty(userAlias))
            {
                MsgHelper.Show("别名不能为空");
                return;
            }
            if (string.IsNullOrEmpty(password))
            {
                MsgHelper.Show("密码不能为空");
                return;
            }

            if (string.IsNullOrEmpty(password2))
            {
                MsgHelper.Show("再次输入密码不能为空");
                return;
            }

            if (password == password2)
            {
                MsgHelper.Show("两次密码要相同");
                return;
            }

            if (string.IsNullOrEmpty(phone))
            {
                MsgHelper.Show("电话号码不能为空");
                return;
            }

            if (this.cmbChooseRole.SelectedIndex < 0)
            {
                MsgHelper.Show("请选择一个角色");
                return;
            }

            DataHelper.SaveUser(userName, userAlias, password, phone, role.RoleId.ToString(), (result) =>
            {
                this.Dispatcher.BeginInvoke(() =>
                {
                    LoadUsers();
                    this.lstUsers.SelectedIndex = -1;
                    this.cmbChooseRole.SelectedIndex = -1;
                    this.txtUserName.Text = string.Empty;
                    this.txtUserAlias.Text = string.Empty;
                    this.txtPassword.Password = string.Empty;
                    this.txtPassword2.Password = string.Empty;
                    this.txtPhone.Text = string.Empty;
                    MsgHelper.ShowSuccess();
                });

            });

        }

        private void EditUser()
        {

            GRole role = this.cmbChooseRole.SelectedItem as GRole;
            GUser user = this.lstUsers.SelectedItem as GUser;
            string userName = this.txtUserName.Text.Trim();
            string userAlias = this.txtUserAlias.Text.Trim();
            string password = this.txtPassword.Password.Trim();
            string password2 = this.txtPassword2.Password.Trim();
            string phone = this.txtPhone.Text.Trim();

            if (string.IsNullOrEmpty(userName))
            {
                MsgHelper.Show("用户名不能为空");
                return;
            }

            if (string.IsNullOrEmpty(userAlias))
            {
                MsgHelper.Show("别名不能为空");
                return;
            }
            if (string.IsNullOrEmpty(password))
            {
                MsgHelper.Show("密码不能为空");
                return;
            }

            if (string.IsNullOrEmpty(password2))
            {
                MsgHelper.Show("再次输入密码不能为空");
                return;
            }

            if (password == password2)
            {
                MsgHelper.Show("两次密码要相同");
                return;
            }

            if (string.IsNullOrEmpty(phone))
            {
                MsgHelper.Show("电话号码不能为空");
                return;
            }

            if (this.cmbChooseRole.SelectedIndex < 0)
            {
                MsgHelper.Show("请选择一个角色");
                return;
            }

            if (this.cmbChooseRole.SelectedIndex < 0)
            {
                MsgHelper.Show("请选择一个角色");
                return;
            }
            if (this.lstUsers.SelectedIndex < 0)
            {
                MsgHelper.Show("请选择一个用户");
                return;
            }

            PathHelper.GetData("AddressPage.aspx?option=" + GConstant.OP_EDIT_USER,
                new Dictionary<string, string>(){
                {"Id",user.Id.ToString()},
                {"UserName",userName},
                {"UserAlias",userAlias},
                {"Password",password},
                {"Phone",phone},
                {"RoleId",role.RoleId.ToString()}
                }, (e) =>
            {
                this.Dispatcher.BeginInvoke(() =>
                {
                    LoadUsers();
                });
            });

        }

        private void DeleteUser()
        {
            if (this.lstUsers.SelectedIndex < -1)
            {
                MsgHelper.Show("请选择一个用户");
                return;
            }
            GUser user = this.lstUsers.SelectedItem as GUser;
            PathHelper.GetData("AddressPage.aspx?option=" + GConstant.OP_DEL_USER,
                new Dictionary<string, string>(){
                {"Id",user.Id.ToString()}
                }, (e) =>
            {
                this.Dispatcher.BeginInvoke(() =>
                {
                    LoadUsers();
                });
            });

        }

        private void lstUsers_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            GUser user = this.lstUsers.SelectedItem as GUser;
            if (user != null)
            {
                GRole role = null;
                foreach (var item in this.cmbChooseRole.Items)
                {
                    var temp = item as GRole;
                    if (temp != null && temp.RoleId == user.Role.RoleId)
                    {
                        role = temp;
                    }
                }

                if (role != null)
                {
                    this.cmbChooseRole.SelectedValue = role;
                }
                this.txtUserName.Text = user.UserName;
                this.txtUserAlias.Text = user.UserAlias;
                this.txtPassword.Password = user.Password;
                this.txtPhone.Text = user.Phone;
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            DeleteUser();
        }
    }// end for class
}

